package com.hongdee.crm.task.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.StringUtil;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.task.entity.TCrmTask;
import com.hongdee.crm.task.entity.TCrmTaskCarVin;
import com.hongdee.crm.task.entity.TCrmTaskReal;
import com.hongdee.crm.task.service.ITCrmTaskManageService;
import com.hongdee.crm.task.service.ITCrmTaskService;
import com.hongdee.crm.task.service.ITCrmTimeTaskService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 名  称:
 * 类  型: class
 * 描  述: 任务web操作接口
 * 路  径:com.hongdee.crm.task.controller
 * 作  者:liuze
 * 日  期:2017/3/7 11:11
 */
@RestController
@RequestMapping("/crm/task")
public class TCRMTaskController {

    //8a1029015a6e4a93015a6eadfe860002 SA
    //8a1029015a6e4a93015a6eaea2790004 客服专员
    //8a1029015a6e4a93015a6eaf47940006 保险接待

    /**
     * 任务权限
     */
    private enum TASKPOWER {
        SEE, HANDLE
    }

    @Autowired
    private ITCrmTaskService taskService;
    @Autowired
    private ITCrmTimeTaskService crmTimeTaskService;

    @Autowired
    private ITCrmTaskManageService manageService;

    /***
     * 任务中心-任务管理-查询
     * @param uid
     * @param token
     * @param pcode
     * @param code
     * @param start
     * @param limit
     * @param title
     * @param deal
     * @param allotstatus
     * @param assignId
     * @param allotType
     * @param allotTypeV
     * @param taskType
     * @param month
     * @param companyId
     * @param series
     * @param acTag
     * @return
     * @throws Exception
     */
    @RequestMapping("/manger/page")
    @ResponseBody
    public Map queryMangerPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "pcode", defaultValue = "") String pcode,
                               @RequestParam(name = "code", defaultValue = "") String code,
                               @RequestParam(name = "start", defaultValue = "0") String start,
                               @RequestParam(name = "limit", defaultValue = "50") String limit,
                               @RequestParam(name = "title", defaultValue = "") String title,
                               @RequestParam(name = "deal", defaultValue = "") String deal,
                               @RequestParam(name = "allotstatus", defaultValue = "") String allotstatus,
                               @RequestParam(name = "assignId", defaultValue = "") String assignId,
                               @RequestParam(name = "allotType", defaultValue = "") String allotType,
                               @RequestParam(name = "allotTypeV", defaultValue = "") String allotTypeV,
                               @RequestParam(name = "taskType", defaultValue = "") String taskType,
                               @RequestParam(name = "month", defaultValue = "") String month,
                               @RequestParam(name = "companyId", defaultValue = "") String companyId,
                               @RequestParam(name = "series", defaultValue = "") String series,
                               @RequestParam(name = "acTag", defaultValue = "") String acTag,
                               @RequestBody Map belongsStoreName,
                               @RequestParam(name = "inWx", defaultValue = "") String inWx
    ) throws Exception {
        if (StringUtils.isEmpty(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        System.out.println(companyId);
        if (!StringUtils.isEmpty(deal)) {
            uid = deal;
        }
        Map map = this.taskService.queryMangerPage(start, limit, companyId, pcode, code, uid, title, deal,
                TASKPOWER.SEE.ordinal(), allotstatus, assignId, allotType, allotTypeV, taskType, month, series, acTag,inWx,belongsStoreName);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


    /**
     * 任务二次指派
     *
     * @param uid
     * @param token
     * @param saId
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/distTask")
    @ResponseBody
    public Map distTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "token", defaultValue = "") String token,
                        @RequestParam(name = "saId", defaultValue = "") String saId,
                        @RequestBody Map data,
                        @RequestParam(name = "pcode", defaultValue = "") String pcode,
                        @RequestParam(name = "code", defaultValue = "") String code,
                        @RequestParam(name = "title", defaultValue = "") String title,
                        @RequestParam(name = "allotstatus", defaultValue = "") String allotstatus,
                        @RequestParam(name = "allotType", defaultValue = "") String allotType,
                        @RequestParam(name = "allotTypeV", defaultValue = "") String allotTypeV,
                        @RequestParam(name = "force", defaultValue = "false") boolean force,
                        @RequestParam(name = "taskType", defaultValue = "") String taskType,
                        @RequestParam(name = "deal", defaultValue = "") String deal,
                        @RequestParam(name = "series", defaultValue = "") String series,
                        @RequestParam(name = "acTag", defaultValue = "") String acTag) throws Exception {
//        Map userMap=TokenUtil.getUser(uid);
        int nums = taskService.distTask(pcode, code, uid, title, TASKPOWER.SEE.ordinal(), allotstatus, allotType,
                allotTypeV, data, saId, force, deal, taskType,series,acTag);
        Map<String, Object> map = new HashMap<>();
        if (nums >= 0) {
            map.put("success", true);
            map.put("msg", "任务指派成功!");
        } else {
            map.put("success", false);
            map.put("msg", "任务未指派成功!");
        }
        return map;
    }


    /**
     * 当天-月度任务-查询
     *
     * @param uid
     * @param token
     * @param pcode    任务大类
     * @param code     任务小类
     * @param status   状态 标识任务未完成0 1已完成 2已关闭
     * @param start    起始点
     * @param limit    条数
     * @param platenum 车牌号
     * @param day      标识当天任务
     * @param month    标识当月任务
     * @param title    任务标题
     * @param deal     处理人
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map query(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestParam(name = "pcode", defaultValue = "") String pcode,
                     @RequestParam(name = "code", defaultValue = "") String code,
                     @RequestParam(name = "status", defaultValue = "0") String status,
                     @RequestParam(name = "start", defaultValue = "0") String start,
                     @RequestParam(name = "limit", defaultValue = "50") String limit,
                     @RequestParam(name = "platenum", defaultValue = "") String platenum,
                     @RequestParam(name = "day", defaultValue = "") String day,
                     @RequestParam(name = "month", defaultValue = "") String month,
                     @RequestParam(name = "title", defaultValue = "") String title,
                     @RequestParam(name = "deal", defaultValue = "") String deal,
                     @RequestParam(name = "vin", defaultValue = "") String vin,
                     @RequestParam(name = "area", defaultValue = "") String area,
                     @RequestParam(name = "companyId", defaultValue = "") String companyId,
                     @RequestParam(name = "queryExpecDate", defaultValue = "") String queryExpecDate
    ) throws Exception {

        if (StringUtils.isEmpty(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        System.out.println(companyId);
        if (!StringUtils.isEmpty(deal)) {
            uid = deal;
        }
        // cid = "8a1029015a227c5a015a2589ab9b0001";
        Map map = this.taskService.dayAndMonthQueryTask(start, limit, companyId, pcode, code, status, uid, day, month, platenum,
                title, vin, area, queryExpecDate);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /******
     * 查询
     * @param uid 用户 ID
     * @param token 用户 Token
     * @param pcode 任务大类编码
     * @param code  任务小类编码
     * @param status 任务状态
     * @param start 开始条数
     * @param limit 每页限制数量
     * @param platenum 车牌号
     * @param expectDate 提醒日期
     * @param day
     * @param month
     * @param title 任务标题
     * @param deal
     * @param vin 车架号
     * @param area 区域
     * @return
     * @throws Exception
     */
    @RequestMapping("/page/wait")
    @ResponseBody
    public Map queryWaitTaskPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "pcode", defaultValue = "") String pcode,
                                 @RequestParam(name = "code", defaultValue = "") String code,
                                 @RequestParam(name = "status", defaultValue = "0") String status,
                                 @RequestParam(name = "start", defaultValue = "0") String start,
                                 @RequestParam(name = "limit", defaultValue = "50") String limit,
                                 @RequestParam(name = "platenum", defaultValue = "") String platenum,
                                 @RequestParam(name = "expectDate", defaultValue = "") String expectDate,
                                 @RequestParam(name = "day", defaultValue = "") String day,
                                 @RequestParam(name = "month", defaultValue = "") String month,
                                 @RequestParam(name = "title", defaultValue = "") String title,
                                 @RequestParam(name = "deal", defaultValue = "") String deal,
                                 @RequestParam(name = "vin", defaultValue = "") String vin,
                                 @RequestParam(name = "area", defaultValue = "") String area,
                                 @RequestParam(name = "companyId", defaultValue = "") String companyId
    ) throws Exception {

        /***
         * 公司不为空时抛出查询异常
         * **/
        if (StringUtils.isEmpty(companyId)) {

            companyId = TokenUtil.getCompanyId(uid);
        }

//        String cid = TokenUtil.getCompanyId(uid);
        //指定任务处理人
        if (!"".equals(deal)) {
            uid = deal;
        }
        Map map = this.taskService.crmCarTaskQuery(uid, start, limit, status, vin, title, pcode, code, expectDate, area);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /******
     *
     * @param uid 用户 ID
     * @param token 用户 Token
     * @param pcode 任务大类编码
     * @param code  任务小类编码
     * @param status 任务状态
     * @param start 开始条数
     * @param limit 每页限制数量
     * @param platenum 车牌号
     * @param expectDate 提醒日期
     * @param day
     * @param month
     * @param title 任务标题
     * @param deal
     * @param vin 车架号
     * @param area 区域
     * @return
     * @throws Exception
     */
    @RequestMapping("/page/finish")
    @ResponseBody
    public Map queryFinishTaskPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "pcode", defaultValue = "") String pcode,
                                   @RequestParam(name = "code", defaultValue = "") String code,
                                   @RequestParam(name = "status", defaultValue = "0") String status,
                                   @RequestParam(name = "start", defaultValue = "0") String start,
                                   @RequestParam(name = "limit", defaultValue = "50") String limit,
                                   @RequestParam(name = "platenum", defaultValue = "") String platenum,
                                   @RequestParam(name = "expectDate", defaultValue = "") String expectDate,
                                   @RequestParam(name = "day", defaultValue = "") String day,
                                   @RequestParam(name = "month", defaultValue = "") String month,
                                   @RequestParam(name = "title", defaultValue = "") String title,
                                   @RequestParam(name = "deal", defaultValue = "") String deal,
                                   @RequestParam(name = "vin", defaultValue = "") String vin,
                                   @RequestParam(name = "area", defaultValue = "") String area
    ) throws Exception {
        String cid = TokenUtil.getCompanyId(uid);
        if (!"".equals(deal)) {
            uid = deal;
        }
//        Map map = this.taskService.crmCarTaskQuery(uid, start, limit, status, vin, title, pcode, code, expectDate, area);
//        map.put("success", true);
//        map.put("msg", "分页查询成功！");
        return null;
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmTask data) throws Exception {
        EntityUtils.valid(data);
        String oid = TokenUtil.getCompanyId(uid);
        TCrmTask task = this.taskService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setData(task);
        res.setSuccess(true);
        res.setMsg("操作成功！");
        return res;
    }

    /**
     * @return List
     * @throws Exception
     * @desc 任务处理下拉：所有可处理任务人的及待处理的任务数
     * @author 张国凯
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token,
                           @RequestParam(name = "companyId", defaultValue = "") String companyId,
                           @RequestParam(name = "area", defaultValue = "") String area,
                           @RequestParam(name = "brand", defaultValue = "") String brand) throws Exception {
        return this.taskService.combo(uid, companyId, area, brand);
    }


    /**
     * 任务预生成-查询
     *
     * @param uid      用户ID
     * @param token
     * @param pcode    大类
     * @param code     小类
     * @param status   状态
     * @param start    开始页
     * @param limit    条数
     * @param platenum 车牌号
     * @param day
     * @param month
     * @param title    标题
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchpage")
    @ResponseBody
    public Map searchpage(@RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestParam(name = "pcode", defaultValue = "") String pcode,
                          @RequestParam(name = "code", defaultValue = "") String code,
                          @RequestParam(name = "status", defaultValue = "0") String status,
                          @RequestParam(name = "start", defaultValue = "0") String start,
                          @RequestParam(name = "limit", defaultValue = "50") String limit,
                          @RequestParam(name = "platenum", defaultValue = "") String platenum,
                          @RequestParam(name = "day", defaultValue = "") String day,
                          @RequestParam(name = "month", defaultValue = "") String month,
                          @RequestParam(name = "title", defaultValue = "") String title,
                          @RequestParam(name = "companyId", defaultValue = "") String companyId

    ) throws Exception {

        if (StringUtils.isEmpty(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        System.out.println(companyId);
        //TODO:测试数据
        Map map = this.taskService.searchTask(code, pcode, title, platenum, companyId, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 任务预生成-确认任务
     * 确认任务
     *
     * @param uid
     * @param token
     * @param pcode
     * @param code
     * @param platenum
     * @param title
     * @return
     * @throws Exception
     */
    @RequestMapping("/transferTask")
    @ResponseBody
    public Map transferTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "pcode", defaultValue = "") String pcode,
                            @RequestParam(name = "code", defaultValue = "") String code,
                            @RequestParam(name = "platenum", defaultValue = "") String platenum,
                            @RequestParam(name = "title", defaultValue = "") String title,
                            @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        //String cid = TokenUtil.getCompanyId(uid);
        if (StringUtils.isEmpty(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        System.out.println(companyId);
        taskService.transferTask(code, pcode, title, platenum, companyId);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "任务处理成功！");
        return map;
    }

    /**
     * 任务预生成-批量确认任务
     * 批量确认任务
     *
     * @param uid
     * @param token
     * @param pcode
     * @param code
     * @param platenum
     * @param title
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/transferTaskBatch")
    @ResponseBody
    public Map transferTaskBatch(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "pcode", defaultValue = "") String pcode,
                                 @RequestParam(name = "code", defaultValue = "") String code,
                                 @RequestParam(name = "platenum", defaultValue = "") String platenum,
                                 @RequestParam(name = "title", defaultValue = "") String title,
                                 @RequestParam(name = "companyId", defaultValue = "") String companyId,
                                 @RequestBody List<Map> data) throws Exception {
        if (StringUtils.isEmpty(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        System.out.println(companyId);
        taskService.transferTask(data, companyId);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "任务处理成功！");
        return map;
    }

    /**
     * 任务预生成-清除任务
     * 清除当前用户所在公司下的临时表中的任务信息
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/cleanTask")
    @ResponseBody
    public Map cleanTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "companyId", defaultValue = "") String companyId

    ) throws Exception {
        if (StringUtils.isEmpty(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        System.out.println(companyId);
        taskService.cleanTask(companyId);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "任务清除成功,请重新生成任务信息！");
        return map;
    }

//    /**
//     * 根据当前用户所在的公司生成任务
//     *
//     * @param uid
//     * @param token
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/createTask")
//    @ResponseBody
//    public Map createTask(@RequestParam(name = "uid", defaultValue = "") String uid,
//                          @RequestParam(name = "token", defaultValue = "") String token,
//                          @RequestParam(name = "startTime", defaultValue = "") String startTime,
//                          @RequestParam(name = "endTime", defaultValue = "") String endTime) throws Exception {
//        String cid = TokenUtil.getCompanyId(uid);
//        taskService.createTask(cid, startTime, endTime);
//        Map<String, Object> map = new HashMap<>();
//        map.put("success", true);
//        map.put("msg", "任务生成成功！");
//        return map;
//    }

    /**
     * 任务预生成-任务统计
     * 统计每个类别下的数量
     *
     * @return
     */
    @RequestMapping("/taskCount")
    @ResponseBody
    public Map taskCount(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "companyId", defaultValue = "") String companyId

    ) throws Exception {
        if (StringUtils.isEmpty(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        System.out.println(companyId);
        List<Map<String, Object>> list = taskService.taskCountByType(companyId);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "任务统计查询成功！");
        map.put("data", list);
        return map;
    }

    /**
     * 检测任务处理权限
     *
     * @param uid
     * @param token
     * @param plateNum
     * @return
     * @throws Exception
     */
    @RequestMapping("/checkTask")
    @ResponseBody
    public Map checkTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "plateNum", defaultValue = "") String plateNum) throws Exception {
        String cid = TokenUtil.getCompanyId(uid);
        boolean flag = taskService.checkTaskPower(uid, cid, plateNum);
        Map<String, Object> map = new HashMap<>();
        if (flag) {
            map.put("success", true);
            map.put("msg", "权限验证通过！");
        } else {
            map.put("success", false);
            map.put("msg", "车辆未分配，请联系管理员！");
        }
        return map;
    }

    /**
     * 更新续保结束时间
     * 徐钦勇
     */
    @RequestMapping("/updInsuranceEndDateQuartz")
    @ResponseBody
    public void updInsuranceEndDateQuartz() throws Exception {
        crmTimeTaskService.updInsuranceEndDateQuartz();
    }


    /**
     * 上传车辆VIN信息
     *
     * @param uid
     * @param token
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    @ResponseBody
    public ResultEntity excelImport(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "upExecl", defaultValue = "upExecl") MultipartFile file) throws Exception {
        ResultEntity res = new ResultEntity();
        manageService.parseExcel(file, uid);
        res.setSuccess(true);
        return res;
    }

    /**
     * 获得上传过的VIN文件列表信息
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/vinFiles")
    @ResponseBody
    public ResultEntity vinFiles(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        ResultEntity res = new ResultEntity();
        List<TCrmTaskCarVin> list = manageService.vinFileLists(uid);
        res.setData(list);
        res.setSuccess(true);
        return res;
    }

    /**
     * 自定义任务接口
     */
    @RequestMapping("/saveCustomer")
    @ResponseBody
    public ResultEntity saveCustomer(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestBody TCrmTaskReal data) throws Exception {
        EntityUtils.valid(data);
        String oid = TokenUtil.getCompanyId(uid);
        TCrmTaskReal task = this.taskService.saveTeskRel(data, uid);
        ResultEntity res = new ResultEntity();
        res.setData(task);
        res.setSuccess(true);
        res.setMsg("操作成功！");
        return res;
    }


    @RequestMapping("/createZhTask")
    @ResponseBody
    public ResultEntity createZhTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "pcode", defaultValue = "") String pcode,
                                     @RequestParam(name = "code", defaultValue = "") String code,
                                     @RequestParam(name = "expectDate", defaultValue = "") String expectDate,
                                     @RequestParam(name = "title", defaultValue = "") String title,
                                     @RequestBody Object[] data) throws Exception {
        List<Map> list = (List<Map>) data[0];
        Map<String, Object> params = (Map<String, Object>) data[1];
        Integer num = manageService.createCarZhToTask(params, uid, pcode, code, expectDate, list, title);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("已生成" + num + "条任务信息！！");
        return res;
    }


    /**
     * 任务计算器生成首保 定保任务
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/createSbAndDbTask")
    @ResponseBody
    public ResultEntity createSbAndDbTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                                          @RequestParam(name = "token", defaultValue = "") String token,
                                          @RequestBody Map data) throws Exception {
        ResultEntity res = new ResultEntity();
        TCrmTaskReal tCrmTask = manageService.createCarSbAndDbTask(uid, data);
        res.setData(tCrmTask);
        res.setSuccess(true);
        return res;
    }

    /**
     * 任务计算器生成首保 定保任务
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveSbAndDbTask")
    @ResponseBody
    public ResultEntity saveSbAndDbTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestBody Map data) throws Exception {
        ResultEntity res = new ResultEntity();
        TCrmTaskReal tCrmTask = manageService.saveCarSbAndDbTask(uid, data);
        res.setData(tCrmTask);
        res.setSuccess(true);
        res.setMsg("保养任务创建成功!!");
        return res;
    }


    /**
     * 清空车辆维修类别
     *
     * @param uid
     * @param token
     * @param companyId
     * @return
     * @throws Exception
     */
    @RequestMapping("/moveWorkorderWxType")
    @ResponseBody
    public ResultEntity moveWorkorderWxType(@RequestParam(name = "uid", defaultValue = "") String uid,
                                            @RequestParam(name = "token", defaultValue = "") String token,
                                            @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        ResultEntity res = new ResultEntity();
        if ("".equals(companyId)) {
            throw new Exception("请输入要清空工单的所属公司ID");
        }
        manageService.moveWorkorderWxType(companyId);
        res.setSuccess(true);
        res.setMsg("清空车辆维修类别成功!!");
        return res;
    }

    /**
     * @param uid
     * @param token
     * @param companyId
     * @return
     */
    @RequestMapping("/executeTask")
    @ResponseBody
    public ResultEntity executeTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        ResultEntity res = new ResultEntity();
        manageService.executeTask(companyId);
        res.setSuccess(true);
        res.setMsg("初始任务执行别成功!!");
        return res;
    }




    /**
     * 当天 当月任务
     *
     * @param uid
     * @param token
     * @param pcode    任务大类
     * @param code     任务小类
     * @param status   状态 标识任务未完成0 1已完成 2已关闭
     * @param start    起始点
     * @param limit    条数
     * @param platenum 车牌号
     * @param day      标识当天任务
     * @param month    标识当月任务
     * @param title    任务标题
     * @param deal     处理人
     * @return
     * @throws Exception
     */
    @RequestMapping("/export")
    @ResponseBody
    public ResultEntity export(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "pcode", defaultValue = "") String pcode,
                               @RequestParam(name = "code", defaultValue = "") String code,
                               @RequestParam(name = "status", defaultValue = "0") String status,
                               @RequestParam(name = "start", defaultValue = "0") String start,
                               @RequestParam(name = "limit", defaultValue = "50") String limit,
                               @RequestParam(name = "platenum", defaultValue = "") String platenum,
                               @RequestParam(name = "day", defaultValue = "") String day,
                               @RequestParam(name = "month", defaultValue = "") String month,
                               @RequestParam(name = "title", defaultValue = "") String title,
                               @RequestParam(name = "deal", defaultValue = "") String deal,
                               @RequestParam(name = "vin", defaultValue = "") String vin,
                               @RequestParam(name = "area", defaultValue = "") String area,
                               @RequestParam(name = "queryExpecDate", defaultValue = "") String queryExpecDate,

                               HttpServletRequest request
    ) throws Exception {
        String cid = TokenUtil.getCompanyId(uid);
        if (!"".equals(deal)) {
            uid = deal;
        }
        start = "0";//从第一条记录开始导出
        limit = "65535";//Execl最大记录
        Map map = this.taskService.dayAndMonthQueryTask(start, limit, cid, pcode, code, status, uid, day, month, platenum,
                title, vin, area, queryExpecDate);
        List<Map<String, Object>> dataList = null;
        if (map.get("dataList") != null) {
            dataList = (List<Map<String, Object>>) map.get("dataList");
        }
        XSSFWorkbook wb = this.taskService.createExcel(dataList, uid);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
        path = path + File.separator + "upload";

        File filepack = new File(path);
        if (!filepack.exists()) {
            filepack.mkdir();
        }
        String filePath = path + File.separator + fileId + ".xlsx";
        System.out.println("文件路径：" + filePath);
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;


    }


    /**
     * 任务管理-导出
     *
     * @param uid
     * @param token
     * @param pcode   任务大类
     * @param code    任务小类
     * @param start   起始点
     * @param limit   条数
     * @param request 车牌号
     * @param month   标识当月任务
     * @param title   任务标题
     * @param deal    处理人
     * @return
     * @throws Exception
     */
    @RequestMapping("/export/manger/task")
    @ResponseBody
    public ResultEntity exportMangerTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestParam(name = "pcode", defaultValue = "") String pcode,
                                         @RequestParam(name = "code", defaultValue = "") String code,
                                         @RequestParam(name = "start", defaultValue = "0") String start,
                                         @RequestParam(name = "limit", defaultValue = "50") String limit,
                                         @RequestParam(name = "title", defaultValue = "") String title,
                                         @RequestParam(name = "deal", defaultValue = "") String deal,
                                         @RequestParam(name = "allotstatus", defaultValue = "") String allotstatus,
                                         @RequestParam(name = "assignId", defaultValue = "") String assignId,
                                         @RequestParam(name = "allotType", defaultValue = "") String allotType,
                                         @RequestParam(name = "allotTypeV", defaultValue = "") String allotTypeV,
                                         @RequestParam(name = "taskType", defaultValue = "") String taskType,
                                         @RequestParam(name = "month", defaultValue = "") String month,
                                         @RequestParam(name = "series", defaultValue = "") String series,
                                         @RequestParam(name = "acTag", defaultValue = "") String acTag,
                                         @RequestParam(name = "inWx", defaultValue = "") String inWx,
                                         @RequestBody Map belongsStoreName,
                                         HttpServletRequest request
    ) throws Exception {

        String cid = TokenUtil.getCompanyId(uid);
        if (!"".equals(deal)) {
            uid = deal;
        }
        start = "0";//从第一条记录开始导出
        limit = "65535";//Execl最大记录
        Map map = this.taskService.queryMangerPage(start, limit, cid, pcode, code, uid, title, deal,
                TASKPOWER.SEE.ordinal(), allotstatus, assignId, allotType, allotTypeV, taskType, month, series, acTag,inWx,belongsStoreName);
        List<Map<String, Object>> dataList = null;
        if (map.get("dataList") != null) {
            dataList = (List<Map<String, Object>>) map.get("dataList");
        }
        XSSFWorkbook wb = this.taskService.createExcel(dataList, uid);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
        path = path + File.separator + "upload";

        File filepack = new File(path);
        if (!filepack.exists()) {
            filepack.mkdir();
        }
        String filePath = path + File.separator + fileId + ".xlsx";
        System.out.println("文件路径：" + filePath);
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;

    }

    /**
     * @Name: 获取当日保险任务的车牌号
     * @Author: Liu XiaoLong
     * @Date: 2017/12/21 15:03
     * @Remark:
     */
    @RequestMapping("/getPlateNum")
    @ResponseBody
    public Map getPlateNum(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token) throws Exception {

        Map map = taskService.getPlateNumForInsuranceTaskToday(uid);
        map.put("success",true);
        map.put("msg","获取车牌号成功");
        return map;
    }
}
